package com.turbo.cloud.minio.web.webservice;

import com.amazonaws.services.s3.model.S3Object;
import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.minio.web.entity.MinioFile;
import com.turbo.cloud.minio.web.service.IMinioFileService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 图形验证码表 前端控制器
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class MinioController {

    final IMinioFileService minioFileService;

    /**
     * 查看存储bucket是否存在
     *
     * @param bucketName 桶名
     * @return Result<Boolean>
     */
    @GetMapping("/webservice/minio/bucketExists")
    public Result<Boolean> bucketExists(String bucketName) {
        boolean bucketExists = minioFileService.bucketExists(bucketName);
        return R.result(bucketExists);
    }

    /**
     * 文件上传
     *
     * @param file       文件
     * @param bucketName 桶名
     * @return Boolean
     */
    @PostMapping(value = {"/webservice/minio/uploadFile", "/interface/minio/uploadFile"})
    public Result<MinioFile> uploadFile(MultipartFile file, String bucketName, String businessType, String businessNo, String uploadBy) {
        MinioFile minioFile = minioFileService.upload(file, bucketName, businessType, businessNo, uploadBy);
        return R.result(minioFile);
    }

    /**
     * 文件上传
     *
     * @param file       文件
     * @param bucketName 桶名
     * @return Boolean
     */
    @PostMapping(value = {"/webservice/minio/uploadFileByAuth"})
    public Result<MinioFile> uploadFileByAuth(MultipartFile file, String bucketName) {
        MinioFile minioFile = minioFileService.uploadByAuth(file, bucketName);
        return R.result(minioFile);
    }

    /**
     * 获取文件路径
     *
     * @param fileId     文件Id
     * @param bucketName 桶名
     * @return Boolean
     */
    @GetMapping("/webservice/minio/getFileUrl")
    public Result<String> getFileUrl(String fileId, String bucketName) {
        String url = minioFileService.getFileUrl(fileId, bucketName);
        return R.result(url);
    }

    /**
     * 获取文件路径
     *
     * @param fileId     文件Id
     * @param bucketName 桶名
     * @param expiry     失效时间（秒）
     * @return Boolean
     */
    @GetMapping("/webservice/minio/getExpiryFileUrl")
    public Result<String> getExpiryFileUrl(String fileId, String bucketName, int expiry) {
        String url = minioFileService.getExpiryFileUrl(fileId, bucketName, expiry);
        return R.result(url);
    }

    /**
     * 获取文件路径
     *
     * @param fileId     文件Id
     * @param bucketName 桶名
     * @return Boolean
     */
    @GetMapping("/webservice/minio/download")
    public void download(HttpServletResponse response, String fileId, String bucketName) {
        minioFileService.download(response, fileId, bucketName);
    }

    /**
     * 删除文件
     *
     * @param fileId     文件Id
     * @param bucketName 桶名
     * @return Boolean
     */
    @GetMapping("/webservice/minio/removeFile")
    public Result<Boolean> removeFile(String fileId, String bucketName) {
        Boolean isOk = minioFileService.remove(fileId, bucketName);
        return R.result(isOk);
    }


    /**
     * 通过bucketName和objectName获取对象
     *
     * @param fileId     文件Id
     * @param bucketName 桶名
     * @return Boolean
     */
    @GetMapping("/webservice/minio/getObject")
    public Result<S3Object> getObject(String fileId, String bucketName) {
        S3Object s3Object = minioFileService.getObject(fileId, bucketName);
        return R.result(s3Object);
    }
}
